//
//  CYYForgetPasswordViewController.m
//  LetsParty
//
//  Created by rimi on 15/9/17.
//  Copyright (c) 2015年 LetsPartyTeam. All rights reserved.
//

#import "CYYForgetPasswordViewController.h"

@interface CYYForgetPasswordViewController ()

@property (nonatomic, strong) UIScrollView * mainScrollView;//主滚动视图
@property (nonatomic, strong) UITextField * emailTextField;//用户名
@property (nonatomic, strong) UIButton * checkButton;//验证
-(void)initializeUserInterface;

@end

@implementation CYYForgetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeUserInterface];
    //监听键盘
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHiden:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    //移除通知
    [[NSNotificationCenter defaultCenter] removeObserver:UIKeyboardWillHideNotification];
    [[NSNotificationCenter defaultCenter] removeObserver:UIKeyboardWillShowNotification];
}

#pragma mark - private methods
-(void)initializeUserInterface{
    self.baseNavigationBar.backgroundColor = [UIColor whiteColor];
    [self.leftNavButton setImage:[UIImage imageNamed:@"iconfont-zuojiantou"] forState:UIControlStateNormal];
    [self.leftNavButton addTarget:self action:@selector(respondsToLeftButton:) forControlEvents:UIControlEventTouchUpInside];
    self.bartitleLabel.text = @"忘记密码";
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.mainScrollView];
    [self.mainScrollView addSubview:self.checkButton];
    [self.mainScrollView addSubview:self.emailTextField];
    [self.checkButton addTarget:self action:@selector(respondsToCheckButton:) forControlEvents:UIControlEventTouchUpInside];
}

- (UITextField *)getTextFieldWithIndex:(NSInteger)index{
    UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(20, 80 + 50 * index, 280, 40) adjustWidth:NO];
    textField.placeholder = @[@"请输入邮箱"][index];
    textField.alpha = 0.8;
    textField.textAlignment = NSTextAlignmentLeft;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.inputAccessoryView = [self customKeyboardToolbar];
    return textField;
}

- (void)alterViewWithMessage:(NSString *)message{
    UIAlertController * alterViewController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    [alterViewController addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        [alterViewController dismissViewControllerAnimated:YES completion:nil];
    }]];
    [self presentViewController:alterViewController animated:YES
                                          completion:nil];
}

//键盘uitoolbar
- (UIToolbar * )customKeyboardToolbar{
    UIToolbar * toolbar = [[UIToolbar alloc]init];
    toolbar.barTintColor = [UIColor whiteColor];
    toolbar.frame = DHFlexibleFrame(CGRectMake(0, 0, 320, 30), YES);
    //隐藏按钮
    UIBarButtonItem * spaceBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    spaceBarButtonItem.width = 200;
    UIBarButtonItem * rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-bohaojianpanshouqi"] style:UIBarButtonItemStylePlain target:self action:@selector(respondsToDoneButton)];
    rightBarButtonItem.tintColor = [UIColor darkGrayColor];
    toolbar.items = @[spaceBarButtonItem,rightBarButtonItem];
    return toolbar;
}

//判断用户名 字母和汉字
- (BOOL)judgeUsername:(NSString *)string{
    NSString * regularString = @"^[a-zA-Z\u4E00-\u9FA5]+$";
    NSPredicate * predicate = [NSPredicate predicateWithFormat:@"self matches %@",regularString];
    //返回值为YES：表明用户名不符合要求
    return ![predicate evaluateWithObject:string];
}

#pragma mark - responds event
- (void)respondsToLeftButton:(UIButton *)button{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)respondsToCheckButton:(UIButton *)button{
    //如果为空返回
    if ([self.emailTextField.text isEqualToString:@""]) {
        [self alterViewWithMessage:@"请输入邮箱地址"];
        return;
    }
     __weak typeof(self) myVC = self;
    //验证信息
    [CYYNetWorkManager changPassworgByEmail:myVC.emailTextField.text sucess:^(NSString *sucessString) {
        [myVC alterViewWithMessage:sucessString];
    } fail:^(NSError *error) {
        [myVC alterViewWithMessage:error.domain];
    }];
    
}

- (void)keyboardWillShow:(NSNotification *)notification{
    //获得键盘高度
    //    [(NSValue *)[notification.userInfo objectForKey:@"UIKeyboardBoundsUserInfoKey"] CGRectValue].size.height
    self.mainScrollView.frame = DHFlexibleFrame(CGRectMake(0, 64, 320, 568 - 100), NO);
    
}

- (void)keyboardWillHiden:(NSNotification *)notification{
    self.mainScrollView.frame = DHFlexibleFrame(CGRectMake(0, 64, 320, 568), NO);
}

//处理键盘隐藏事件
- (void)respondsToDoneButton{
    [self.view endEditing:YES];
}

#pragma mark - getter
- (UIScrollView *)mainScrollView{
    if (!_mainScrollView) {
        _mainScrollView = ({
            UIScrollView * scrollView = [[UIScrollView alloc]init];
            scrollView.frame = DHFlexibleFrame(CGRectMake(0, 64, 320, 504), NO);
            scrollView.contentSize = DHFlexibleSize(CGSizeMake(320, 504), NO);
            scrollView.bounces = NO;
            scrollView;
        });
    }
    return _mainScrollView;
}

-(UITextField *)emailTextField{
    if (!_emailTextField) {
        _emailTextField = ({
            [self getTextFieldWithIndex:0];
        });
    }
    return _emailTextField;
}

- (UIButton *)checkButton{
    if (!_checkButton) {
        _checkButton = ({
            UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = DHFlexibleFrame(CGRectMake(20, 180 + 50, 280, 40), NO);
            button.backgroundColor = COLOR_TagBlue;
            button.layer.cornerRadius = 20 * DHFlexibleVerticalMutiplier();
            [button setTitle:@"验证" forState:UIControlStateNormal];
            button;
        });
    }
    return _checkButton;
}

@end
